package com.tensai.study.新概念.响应式编程.reactor.flux;

import org.junit.Test;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;

/**
 * @author Tensai
 */
public class PublishOn {

    public static int F = 5;

    private static void cOut(String tag, Object i) {
        System.out.println(Thread.currentThread().getName() + "\t" + tag + "\t" + i + "\t" + Thread.currentThread().getName());
    }

    @Test
    public void test() {
        Flux
                //创建消费者
                .create(emitter -> {
                            for (int i = 0; i <= F; i++) {
                                if (emitter.isCancelled()) {
                                    return;
                                }
                                cOut(">>>", i);
                                emitter.next(i);
                            }
                        }
                )
                .doOnNext(s -> cOut("<<<", s))
                .publishOn(Schedulers.parallel())
                .subscribe(customer -> cOut("|||", customer));
    }

    @Test
    public void test1() {
        Flux.range(1, 10000)
                .publishOn(Schedulers.parallel())
                .subscribe(customer -> cOut("|||", customer));
        System.out.println(Thread.currentThread().getName());
    }
}
